package ch.jamme.conf.def;

import java.util.*;

import ch.jamme.conf.*;

/**
 * Defines a strategy to decide whether a given field should be treated as a primitive (a single plain value) field 
 * or as a complex one that needs to be recursively introspected in its own fields
 * @author Mario Fusco
 */
public class DefaultFieldPrimitiveStrategy implements FieldPrimitiveStrategy {

	/**
	 * Returns true if a field belonging to the given Class and having the given name and value 
	 * should be considered as primitive and then no furtherly introspected
	 * @param containingClass The class of the object containing the field to be marshalled
	 * @param name The name of the field to be marshalled
	 * @param value The value of the field to be marshalled
	 * @return True if the field belonging to the given Class and with the given name and value is a primitive one
	 */
	public boolean isPrimitive(Class<?> containingClass, String name, Object value) {
		return isPrimitive(value.getClass());
	}

	/**
	 * Returns true if a field of the given Class should be considered as primitive and then no furtherly introspected
	 * @param fieldClass The Class of the field to be declared as primitive or not
	 * @return True if the given Class is a primitive one
	 */
	public boolean isPrimitive(Class<?> fieldClass) {
		if (fieldClass.isPrimitive()) return true;
		if (fieldClass.isEnum()) return true;
		if (String.class.isAssignableFrom(fieldClass)) return true;
		if (Number.class.isAssignableFrom(fieldClass)) return true;
		if (Date.class.isAssignableFrom(fieldClass)) return true;
		if (Boolean.class.isAssignableFrom(fieldClass)) return true;
		return false;
	}
}
